package com.example.Servlet;

import com.example.Function.DataBaseOp;
import com.example.Object.Question;
import com.google.gson.Gson;
import com.google.gson.JsonArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author Echo
 * 得到详细题型
 */
@WebServlet("/GetQuestionDetail")
public class GetQuestionDetail extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        String code = req.getParameter("code");
        String email = req.getParameter("email");
        String pattern = req.getParameter("pattern");
        List<Question> questionList = new ArrayList<>();
//        System.out.println("模式 : " + pattern + "代码：" + code);
        if("normal".equals(pattern) || "random".equals(pattern)) {
            questionList = DataBaseOp.getQuestionList(code);
            if ("random".equals(pattern)) {
                Collections.shuffle(questionList);
            }
        }else{
            //拿错题
            questionList = DataBaseOp.getErrorQuestionList(DataBaseOp.getErrorCode(code),email);
        }
        PrintWriter out = resp.getWriter();

        Gson gson1 = new Gson();

        out.println(gson1.toJson(questionList));


    }
}
